define(['ojs/ojcore', 'knockout', 'jquery', 'ojs/ojknockout', 'promise', 'ojs/ojinputtext', 'ojs/ojdatetimepicker', 'ojs/ojselectcombobox', 'ojs/ojcheckboxset', 'ojs/ojtable', 'ojs/ojdatacollection-utils', 'ojs/ojarraytabledatasource'], function(oj, ko, $) {
    function reviewModule() {
        var opModule = {
            defaulePage:1,
            currentPage:ko.observable(1),
            fileCurrentPage:ko.observable(1),
            totalPage:10,
            flag:true,
            flag1:false,
            files:'aaaaaaa',
            filepath:'',
            actionmes:ko.observable({
                servelist:['192.168.1.1','192.168.1.2'],
                fileurl:'aaa'
            }),
            getCdnCounts:function(){
                return 95;
                this.totalPage = 10;
            },
            pageBefore:function(){
                var a = this.currentPage();
                a>1?a--:1;
                this.currentPage(a);
                this.flag?this.getServeList():'' ;
                this.flag = a==1?false:true;
                a = null;
                // console.log(this.currentPage());
            },
            pageAfter:function(){
                var a = this.currentPage();
                var b = this.totalPage;
                a<b?a++:b;
                this.currentPage(a);
                this.flag?this.getServeList():'' ;
                this.flag = a==b?false:true;
                a = null;
                b = null;
                // console.log(this.currentPage());
            },
            pageFirst:function(a){
                console.log(a);
                this.currentPage(1);
                this.flag ? this.getServeList(): '';
                this.flag = false;
            },
            pageLast:function(){
                this.currentPage(this.totalPage);
                this.flag?this.getServeList():'' ;
                this.flag = false;
            },
            getServeList:function(){
                var self = this;
                $.ajax({
                     type:"get",
                     url: "js/json/serve.json",
                     data:{currentPage:self.currentPage},
                     success: function(data){
                        console.log(data);
                        self.deptArray(data);
                     }
                 });
            },
            getFileList:function(){
                var self = this;
                $.ajax({
                     type:"get",
                     url: "js/json/tree.json",
                     data:{currentPage:self.currentPage},
                     success: function(data){
                        console.log(data);
                        self.fileList(data.data);
                        $('.checkFile').on('click',function(){
                            console.log('aaaaaaaaaaaaaaa');
                            $('.checkFile').prop('checked',false);
                            $(this).prop('checked',true);
                        });
                        $('.treeClick').on('click',function(){
                            if($(this).html()=='打开'){
                                var treeindex = $(this).index('.treeClick');
                                var path = '';
                                path += $('.file_path').eq(treeindex).html();
                                console.log(path);
                                self.filepath += path;
                                $.ajax({
                                    type:'get',
                                    url:'js/json/blob.json',
                                    data:{
                                        filePath:path
                                    },
                                    success:function(data){
                                        self.fileList(data.data);
                                        $('.checkFile').on('click',function(){
                                            console.log('aaaaaaaaaaaaaaa');
                                            $('.checkFile').prop('checked',false);
                                            $(this).prop('checked',true);
                                        });
                                        alert('1');
                                    }
                                });
                            }
                        });
                     }
                 });
            },
            diagoClose:function(){
                $('#actionMes').hide('fast');
                $('#mask').hide('fast');
            },
            serveAdd:function(){

            },
            serveClose:function(){

            },
            checkall:function(){ 
                if(this.flag1){
                    $('.checkServe').prop('checked',false);
                    this.flag1 = false;
                }else{
                    $('.checkServe').prop('checked',true);
                    this.flag1 = true;
                }
                console.log($('.item_selected:checked'));
            },
            checkSingle:function(){
                // $('.checkServe').prop('checked',false);
                // console.log($(this));
                // $(this).find('.checkServe').prop('checked',true);
            },
            fileList:ko.observableArray([
                    {
                        name:'jia',
                        id:'a/a',
                        type:'aaaaaaaa'
                    }
                ]),
            deptArray:ko.observableArray([
                    {
                        assetId:'001',
                        ipAddress:'192.168.1.1',
                        userName:'gy',
                        password:'1234',
                    }
                ]
                )
        }
        this.handleAttached =function(){
            console.log('handleAttached');
            opModule.getServeList(opModule.currentPage);
            opModule.getFileList();
            ko.cleanNode(document.getElementById('playbook_op'));
            ko.applyBindings(opModule,document.getElementById('playbook_op'));
            $('#action').on('click',function(){
                if(($('.checkServe:checked').length==0)||($('.checkFile:checked').length==0)){
                    alert('请选择服务器和playbook进行操作');
                }else{
                    $('#mask').show('fast');
                    $('#actionMes').show('fast');
                    var servelist = [];
                    for(var i = 0 ; i<$('.checkServe:checked').length;i++){
                        var a = $('.checkServe:checked').eq(i).index('.checkServe');
                        console.log(a);
                        servelist.push($('.ipaddress').eq(a).html());
                    }
                    var fileindex = $('.checkFile:checked').index('.checkFile');
                    console.log(fileindex);
                    var html = '';
                    var file = '';
                    for(var i = 0 ,j = servelist.length; i < j ; i++){
                        html += servelist[i] + '<br>';
                    }
                    $('.change_ip_list').html(html);
                    $('.action_file_list').text($('.file_path').eq(fileindex).html());
                    file = $('.file_path').eq(fileindex).html();
                    console.log($('.file_path').eq(fileindex).html())
                    console.log(servelist);
                    $('#execute').on('click',function(){});
                    $('#execute').on('click',function(){
                        console.log(servelist);
                        servelist = servelist + '';
                        $('#mask').hide('fast');
                        $('#actionMes').hide('fast');
                        $.ajax({
                            type:'get',
                            url:'runPlayBook.do',
                            data:{
                                hosts:servelist,
                                filePath:opModule.filepath+'/'+file
                            },
                            success:function(data){
                                alert(data.data);
                                window.location.reload();
                            }
                        })
                    })
                }
                
            });
        }
    // ko.applyBindings(homeViewModule, document.getElementsByClassName('content')[0]);
    
    }
return reviewModule;
});

